package com.lagou;

import java.util.*;
import java.util.stream.Collectors;

public class Count implements Runnable{

    List<ExcuteTime> excuteTimes;

    public Count(List<ExcuteTime> excuteTimes) {
        this.excuteTimes = excuteTimes;
    }
    Date ChangeCurrentTime = new Date();
    Date currentdate= new Date();
    @Override
    public void run() {
        //开启定时任务，对最近的一分钟数据进行统计
        int size = excuteTimes.size();
        if(size < 1){
            return;
        }
        sortObject(excuteTimes,"sayHello");
        sortObject(excuteTimes,"sayHello2");
        sortObject(excuteTimes,"sayHello3");

    }
        private  void sortObject(List<ExcuteTime> excuteTime,String method) {
        //Comparator<ExcuteTime> byNameDESC = Comparator.comparing(ExcuteTime::doTime).reversed();

            //按执行时间排倒叙
        ArrayList<ExcuteTime> excuteTimes = new ArrayList<>();
        int size = excuteTime.size();
        for(int i = 1; i < size ; i++){
        ExcuteTime excuteTime1 = excuteTime.get(i);
        Date doTime = excuteTime1.getDoTime();
         currentdate = new Date();
           //获取一分钟内的执行记录
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(currentdate);
            // 将现在的时间滚动固定时长,转换为Date类型赋值
            calendar.add(Calendar.MINUTE, -1);
            // 转换为Date类型再赋值
             ChangeCurrentTime = calendar.getTime();
            boolean after = ChangeCurrentTime.after(doTime);
            boolean equals = method.equals(excuteTime1.getDoMethod());
            if(!after && equals){
        excuteTimes.add(excuteTime1);
        }
        }
            int size2 = excuteTimes.size();
        if(size2 < 1){
            return;
        }
            //按用时排序
        Comparator<ExcuteTime> byTime = Comparator.comparing(ExcuteTime::getTime);
        List<ExcuteTime> result = excuteTimes.stream().sorted(byTime).collect(Collectors.toList());
       for(ExcuteTime s:   result ){
           System.out.println("实现列表执行时间"+s.getDoTime()+"时长"+s.getTime()+s.getDoMethod());
       }
        int size1 = result.size();
        int tp90 = (size1 * 9) / 10;
        int tp99 = (size1 * 99) / 100;
        System.out.println("tp90为"+result.get(tp90).getTime()+"统计时间"+currentdate+"-"+ChangeCurrentTime+result.get(tp90).getDoMethod());
            System.out.println("tp99为"+result.get(tp99).getTime()+"统计时间"+currentdate+"-"+ChangeCurrentTime+result.get(tp99).getDoMethod());
    }



}
